package com.sjh.lib_update

import android.R.attr.versionCode
import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Build
import android.os.Build.VERSION_CODES


object ApkUtil {
    /**
     * 获取包大小
     * @param size 大小
     * @return 返回单位为 MB
     */
     fun getApkSize(size: Long) = String.format("%.2f MB", size / (1024f * 1024f))


    /**
     * 获取应用版本代号
     * @param context 上下文
     */
    fun getApplicationVersionCode(context: Context): Int {
        val packageManager: PackageManager
        val packageInfo: PackageInfo
        try {
            packageManager =
                context.packageManager
            packageInfo = packageManager.getPackageInfo(
                context.packageName,
                0
            )
           return  if (Build.VERSION.SDK_INT >= VERSION_CODES.P) {
               packageInfo.longVersionCode.toInt() // avoid huge version numbers and you will be ok
            } else {
               packageInfo.versionCode
            }
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return 0
    }


}